package com.cf.conf;

import com.cf.conf.controller.ConfigController;
import com.cf.conf.core.ConfigSetAndUpdateBeanPostprocessor;
import com.cf.conf.core.PropertyManager;
import com.cf.conf.core.UpdatePusher;
import com.cf.conf.core.ZConfProperty;
import com.cf.conf.core.model.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * game-server-DisConfig
 * 分布式配置配置
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/8 下午 03:46
 */
@Slf4j
@Configuration
public class DisBackConfConfig implements InitializingBean {

    @Bean
    public PropertyManager propertyManager() {
        log.info("实例化配置属性管理器！PropertyManager");
        return new PropertyManager();
    }

    @Bean
    public ConfigSetAndUpdateBeanPostprocessor configSetAndUpdateBeanPostprocessor() {
        log.info("实例化属性动态检测器！ConfigSetAndUpdateBeanPostprocessor");
        return new ConfigSetAndUpdateBeanPostprocessor();
    }

    @Bean
    public ConfigService configService() {
        log.info("实例化config持久化器");
        return new ConfigService();
    }


    //##############################redis 动态检测 pusher ######################################

    @Bean
    public UpdatePusher updatePusher() {
        log.info("实例化UpdatePusher");
        return new UpdatePusher();
    }

    @Bean
    @ConditionalOnProperty(value = "zmm.conf.web", havingValue = "true")
    public ConfigController configController() {
        log.info("实例化ConfigController");
        return new ConfigController();
    }


    @Autowired
    private FreeMarkerProperties freeMarkerProperties;

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] templateLoaderPath = freeMarkerProperties.getTemplateLoaderPath();
        ArrayList<String> paths = new ArrayList();
        paths.addAll(Arrays.asList(templateLoaderPath));
        paths.add("classpath:/template-config/");
        String[] strings = paths.toArray(new String[paths.size()]);
        freeMarkerProperties.setTemplateLoaderPath(strings);
    }
}
